package tij4.concurrent.exercise.ex12;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class P12Main01 implements Runnable{

    private int i = 0;

    //返回i的值
    public synchronized int getValue() {
        return i;
    };
    //偶数自增
    private synchronized void evenIncrment() {
        i++;
        Thread.yield();
        i++;
    };
    //一直执行偶数自增方法
    @Override
    public void run() {
        while (true)
            evenIncrment();
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        P12Main01 p12Main01 = new P12Main01();
        executorService.execute(p12Main01);
        while (true) {
            int val = p12Main01.getValue();
            if (val % 2 != 0) {
                System.out.println(val);
                System.exit(0);
            }
        }

    }
}
